package com.caoxw.Android.CityBus;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class DownDataAdapt extends BaseAdapter {
	private LayoutInflater m_Inflater;
	private List<Map<String, Object>> m_Data;

	public DownDataAdapt(Context context) {
		m_Inflater = LayoutInflater.from(context);
		m_Data = new ArrayList<Map<String, Object>>();
	}
	public void addItem(Map<String, Object> object){
		m_Data.add(object);
	}
	public void clear(){
		m_Data.clear();
	}
	public void addItem(String Name,String OtherInfo,boolean isSelected,int Progress){
		Map<String, Object> object = new HashMap<String, Object>();
		object.put("title", Name);
		object.put("otherInfo", OtherInfo);
		object.put("isSelected", Boolean.valueOf(isSelected));
		object.put("progress", Integer.valueOf(Progress));
		m_Data.add(object);
		//notifyDataSetChanged();
	}
	public void setItem(int position,Map<String, Object> object){
		m_Data.set(position, object);
	}
	
	public void setSubItem(int position,String key, Object value){
		m_Data.get(position).put(key, value);
	}
	public Object setSubItem(int position,String key){
		return m_Data.get(position).get(key);
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return m_Data.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return m_Data.get(position);
	}
	public void setText(int position,String value){
		m_Data.get(position).put("title", value);
	}
	public void setProgress(int position,Integer value){
		m_Data.get(position).put("progress", value);
	}
	public void setOtherInfo(int position,String value){
		m_Data.get(position).put("otherInfo", value);
		notifyDataSetChanged();
	}
	public void setSelected(int position,boolean selected){
		m_Data.get(position).put("isSelected", Boolean.valueOf(selected));
	}
	public boolean getSelected(int position){
		return ((Boolean)(m_Data.get(position).get("isSelected"))).booleanValue();
	}
	public void switchSelected(int position){
		m_Data.get(position).put("isSelected", Boolean.valueOf(!((Boolean)(m_Data.get(position).get("isSelected"))).booleanValue()));
		
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null; // convertView为null的时候初始化convertView。
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = m_Inflater.inflate(R.layout.list_item_citydata, null);
			holder.title = (TextView) convertView.findViewById(R.id.Name);
			holder.otherInfo = (TextView) convertView.findViewById(R.id.OtherInfo);
			holder.cBox = (CheckBox) convertView.findViewById(R.id.cb);
			holder.Progress = (ProgressBar) convertView.findViewById(R.id.progress_horizontal);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		//if(((Integer)m_Data.get(position).get("progress")).intValue()==0)
			//holder.Progress.setVisibility(ProgressBar.GONE);
		holder.title.setText(m_Data.get(position).get("title").toString());
		holder.otherInfo.setText(m_Data.get(position).get("otherInfo").toString());
		holder.cBox.setChecked(((Boolean)(m_Data.get(position).get("isSelected"))).booleanValue());
		holder.Progress.setProgress(((Integer)m_Data.get(position).get("progress")).intValue());
		return convertView;
	}

	public final class ViewHolder {
		public TextView title;
		public TextView otherInfo;
		public CheckBox cBox;
		public ProgressBar Progress;
	}
	

}
